package cn.fws.meap.community.web;

import java.util.List;

import cn.fws.framework.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.fws.meap.community.entity.Evaluate;
import cn.fws.meap.community.service.IEvaluateService;
import cn.fws.framework.web.contoller.BaseController;
import cn.fws.framework.web.page.PagingResult;
import cn.fws.framework.web.contoller.JsonResponse;

/**
 * 点赞 信息操作处理
 *
 * @author chenkh
 * @date 2019-03-07
 */
@RestController
@RequestMapping("/meap/evaluate")
@Api(tags = "点赞 信息操作处理")
public class EvaluateController extends BaseController{

	@Autowired
	private IEvaluateService evaluateService;

	/**
	 * 查询点赞列表
	 */
	//@RequiresPermissions("meap:evaluate:list")
	@ApiOperation(value = "查询点赞列表")
	@PostMapping("/list")
	public PagingResult<Evaluate> list(Evaluate evaluate){
		startPage();
        List<Evaluate> list = evaluateService.selectList(evaluate);
		return getPagingResult(list);
	}


	/**
	 * 新增保存点赞
	 */
	//@RequiresPermissions("meap:evaluate:add")
	@ApiOperation(value = "新增保存点赞")
	@ApiImplicitParam(name = "evaluate", value = "新增保存点赞",dataType = "Evaluate")
	@PostMapping("/add")
	public JsonResponse addSave(Evaluate evaluate)	{
		if(StringUtils.isEmpty(evaluate.getArticleType())){
			return toAjax(0,"1","点赞类型不能为空");
		}else if(StringUtils.isEmpty(evaluate.getArticleId())){
			return toAjax(0,"1","文章id不能为空");
		}else if(StringUtils.isEmpty(evaluate.getOpenid())){
			return toAjax(0,"1","用户id不能为空");
		}
		//查询当前用户有没有点赞过这个文章
			Boolean flag=evaluateService.selectOpenId(evaluate.getOpenid(),evaluate.getArticleId());
		System.out.println(flag);
		if (flag){
			return JsonResponse.success("这个文章点过赞",flag);
		}else {
			return toAjax(evaluateService.insert(evaluate));
		}
	}

	/**
	 * 修改保存点赞
	 */
	//@RequiresPermissions("meap:evaluate:edit")
	@ApiOperation(value = "修改保存点赞")
	@ApiImplicitParam(name = "evaluate", value = "修改保存点赞",dataType = "Evaluate")
	@PutMapping("/edit")
	public JsonResponse editSave(Evaluate evaluate)	{
		return toAjax(evaluateService.update(evaluate));
	}

	/**
	 * 删除点赞
	 */
	//@RequiresPermissions("meap:evaluate:remove")
	@ApiOperation(value = "删除点赞")
	@ApiImplicitParam(name = "ids", value = "根据id删除点赞",dataType = "String")
	@DeleteMapping( "/remove")
	public JsonResponse remove(@RequestBody String ids)	{
		System.out.println(ids);
		return toAjax(evaluateService.deleteByIds(ids));
	}
	
}
